package com.maimao.model.cart;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author MaoLin Wang
 * @date 2021/1/9 10:56 上午
 */
@Document(collection = "mm_cart")
@Setter
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ShoppingCart implements Serializable {

    private static final long serialVersionUID = 7728501514272174360L;

    @Id
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long id;

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @Field("user_id")
    private Long userId;
    /**
     * id 通过店铺id聚合购物车
     */
    @Field("shop_id")
    @NotNull(message = "参数非法")
    @Min(message = "参数非法", value = 1)
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long shopId;

    @Field("shop_name")
    @NotBlank(message = "店铺名为空")
    private String shopName;

    private String picture;

    @Field("sku_id")
    @NotNull(message = "规格id非法")
    @Min(value = 1, message = "规格id非法")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long skuId;

    @Field("goods_id")
    @NotNull(message = "商品id非法")
    @Min(value = 1, message = "商品id非法")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long goodsId;

    /**
     * 商品名称 非sku标题
     */
    @NotBlank(message = "标题为空")
    private String name;

    /**
     * 规格值json字符串 仅用于展示 key：规格名 value：规格值
     */

    @Field("spec_str")
    @NotBlank(message = "参数非法")
    private String specStr;

    /**
     * 当前选中的规格参数和位置 前端通过所有索引值 0_0_0 可以找到对应的sku { 规格id：规格项的索引 }
     */
    @Field("selected_param")
    @NotBlank(message = "参数非法")
    private String selectedParam;

    /**
     * 添加到购物车时的单个商品价格
     */
    @NotNull(message = "价格非法")
    private BigDecimal price;

    /**
     * 总价格
     */
    @Field("total_price")
    private BigDecimal totalPrice;

    /**
     * 商品数量
     */
    @NotNull(message = "数量非法")
    @Min(message = "非法数量", value = 1)
    private Integer num;


    /**
     * 如果下架或没货了则为无效商品，前端置灰
     */
    private Boolean valid;

    @Field("modify_time")
    private LocalDateTime modifyTime;

    /**
     * 品牌id 确认订单页使用
     */
    @Transient
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long brandId;
    /**
     * 赠送积分数 确认订单页使用
     */
    @Transient
    private Boolean giveIntegral;

    @Override
    public String toString() {
        return "ShoppingCart{" +
                "id=" + id +
                ", userId=" + userId +
                ", shopId=" + shopId +
                ", shopName='" + shopName + '\'' +
                ", picture='" + picture + '\'' +
                ", skuId=" + skuId +
                ", goodsId=" + goodsId +
                ", name='" + name + '\'' +
                ", specStr='" + specStr + '\'' +
                ", selectedParam='" + selectedParam + '\'' +
                ", price=" + price +
                ", totalPrice=" + totalPrice +
                ", num=" + num +
                ", valid=" + valid +
                ", modifyTime=" + modifyTime +
                ", brandId=" + brandId +
                ", giveIntegral=" + giveIntegral +
                '}';
    }
}
